package com.wodsy.phoenix.datadao.shoppingcart.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.wodsy.phoenix.datadao.shoppingcart.dao.ShoppingcartItemRedisDao;
import com.wodsy.phoenix.dataobject.shoppingcart.model.ShoppingcartItem;


@Component("shoppingcartItemRedisDao")
public class ShoppingcartItemRedisDaoImpl  implements ShoppingcartItemRedisDao {

	@Autowired
    protected RedisTemplate<String, String> redisTemplate;
	

	@Override
	public List<ShoppingcartItem> getMyShoppingcartItemListByUid(Integer userId) {
		Set<Object> set= redisTemplate.opsForHash().keys(userId+"");
		List<ShoppingcartItem> sciList = new ArrayList<ShoppingcartItem>();
		for (Object object : set) {
			Map<String, ShoppingcartItem> map = (Map<String, ShoppingcartItem>) redisTemplate.opsForHash().get(userId+"", object);
			sciList.addAll(map.values());
		}
		return sciList;
	}

	@Override
	public void addMyShoppingcartItem(Integer userId,ShoppingcartItem shoppingcartItem) {
		Object object = checkIfShopExists(userId,shoppingcartItem);
		
		String hashKey = shoppingcartItem.getShopId()+"";
		String mapKey = shoppingcartItem.getSkuId()+"";
		Map<String, ShoppingcartItem> maps = null;
		if(StringUtils.isEmpty(object)){
			maps = new HashMap<String, ShoppingcartItem>();
			maps.put(mapKey, shoppingcartItem);
			redisTemplate.opsForHash().put(userId+"", hashKey, maps);
		}else{
			maps = (Map<String, ShoppingcartItem>) object;
			if(maps.keySet().contains(mapKey)){
				ShoppingcartItem item = maps.get(mapKey);
				item.setSkuCount(item.getSkuCount()+shoppingcartItem.getSkuCount());
				maps.put(mapKey, item);
				redisTemplate.opsForHash().put(userId+"", hashKey, maps);
			}else{
				maps.put(mapKey, shoppingcartItem);
				redisTemplate.opsForHash().put(userId+"", hashKey, maps);
			}
		}
	}
	
	@Override
	public void updateShoppingcartItem(Integer userId,ShoppingcartItem shoppingcartItem) {
		Object object = checkIfShopExists(userId,shoppingcartItem);
		String hashKey = shoppingcartItem.getShopId()+"";
		String mapKey = shoppingcartItem.getSkuId()+"";
		Map<String, ShoppingcartItem> maps = (Map<String, ShoppingcartItem>) object;
		if(maps.isEmpty()){
			return;
		}
		maps.put(mapKey, shoppingcartItem);
		redisTemplate.opsForHash().put(userId+"", hashKey, maps);
	}

	@Override
	public void delShoppingcartItem(Integer userId,Integer shopId,Integer skuId) {
		Object object = redisTemplate.opsForHash().get(userId+"", shopId+"");
		Map<String, ShoppingcartItem> maps = (Map<String, ShoppingcartItem>) object;
		if(maps.isEmpty()){
			return;
		}
		maps.remove(skuId+"");
		redisTemplate.opsForHash().put(userId+"", shopId+"", maps);
	}
	/**
	 * 查看购物车中是否存在某shop的商品
	 * @param userId
	 * @param shoppingcartItem
	 * @return
	 */
	private Object checkIfShopExists(Integer userId,ShoppingcartItem shoppingcartItem){
		String hashKey = shoppingcartItem.getShopId()+"";
		Object object = redisTemplate.opsForHash().get(userId+"", hashKey);
		return object;
	}
    
}
